﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using MySQLStudio.BusinessLogic;
using Telerik.WinControls.UI;

namespace MySQLStudio.GUI.Controls.Table
{
    public partial class TableDockWindow : Telerik.WinControls.UI.Docking.DocumentWindow
    {
        private TableControl m_tableControl;
        private EventHandler<TableSavedEventArgs> m_TableSaved;        

        public TableDockWindow(Host host, Database database, MySQLStudio.BusinessLogic.Table table)
        {
            this.m_tableControl = new TableControl();
            this.m_tableControl.Host = host;
            this.m_tableControl.Database = database;            
            this.m_tableControl.Table = table;

            this.m_tableControl.TableSaved += new EventHandler<TableSavedEventArgs>(m_tableControl_TableSaved);
            
            this.Controls.Add(this.m_tableControl);    
        }

        public event EventHandler<TableSavedEventArgs> TableSaved
        {
            add
            {
                this.m_TableSaved += value;
            }
            remove
            {
                this.m_TableSaved -= value;
            }
        }

        private void OnTableSaved(TableSavedEventArgs e)
        {
            if (this.m_TableSaved != null)
            {
                this.m_TableSaved(this, e);
            }
        }

        public Host Host
        {
            get
            {
                return this.m_tableControl.Host;
            }           
        }

        public Database Database
        {
            get
            {
                return this.m_tableControl.Database;
            }            
        }

        public MySQLStudio.BusinessLogic.Table Table
        {
            get
            {
                return this.m_tableControl.Table;
            }            
        }        

        public override string Text
        {
            get
            {
                if (this.m_tableControl != null && this.Table != null)
                {
                    return this.Table.Name;
                }

                return "new_table";                               
            }
            set
            {
                base.Text = value;
            }
        }

        void m_tableControl_TableSaved(object sender, TableSavedEventArgs e)
        {
            this.Text = this.Table.Name;

            this.OnTableSaved(e);            
        }
    }
}
